/**
 * Created by Administrator on 2018/11/12.
 */
function agreementList($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q, fileListService) {
    $$scope = $scope;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;
    $fileListService = fileListService;
    $$scope.select = [
        {name: '已完成', code: "YES"},
        {name: '未完成', code: "NO"}
    ];

    var dataInit = $CertusJsonAjax.dataInit("purchase/agreementList", {statProcess: "YES"}, agreementColumns, 'agreementContainer', 'agreementToolBarContainer');

    $$scope.stateChange = function (a) {
        var id = $("#purchaseNoId").val();
        dataInit.parameters = {
            purchaseNoId: id,
            statProcess: $$scope.statProcess
        }
        dataInit.refresh(true);
    }

    $$scope.select = [
        {name: '已完成', code: "YES"},
        {name: '未完成', code: "NO"},
        {name: '终结', code: "SHUT"}
    ];

    $$scope.statProcess = "YES"

    $$scope.searchByPurchaseNo = function () {
        var id = $("#purchaseNoId").val();

        dataInit.parameters = {
            purchaseNoId: id,
            statProcess: $$scope.statProcess
        }
        dataInit.refresh(true);
    }
}

function agreementAdd($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q, fileListService, backlogPersonList) {
    $$scope = $scope;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;
    $fileListService = fileListService;
    $backlogPersonList = backlogPersonList;
    //初始化表格数据
    $CertusFormAjax.ajax("purchase/purchaseItemByBrcode", {brCode: $routeParams.brCode}, function (res) {
        if (res.data) {

            $$scope.agreement = {}
            $$scope.agreement.buyingRequisitionCode = $routeParams.brCode;
            //请购单 数据

            var pltlogOption = {
                lang: 'zh-cn',
                ajaxLoad: false,
                // loadURL:'url',
                exportFileName: '请购清单',
                datas: res.data.buyingItem,
                columns: pltlogColumns,
                gridContainer: 'purchaselistContainer',
                toolbarContainer: 'purchaselistToolBarContainer',
                tools: '',
                pageSize: 10,
                pageSizeLimit: [10, 15, 20]
            };
            var pltlogGrid = $.fn.dlshouwen.grid.init(pltlogOption);
            pltlogGrid.load();

            //比价单信息
            //请购单 数据
            var pltlogOption1 = {
                lang: 'zh-cn',
                ajaxLoad: false,
                // loadURL:'url',
                exportFileName: '请购清单',
                datas: res.data.purchaseComparition,
                columns: supplierColumns,
                gridContainer: 'comparisionListId',
                toolbarContainer: 'comparisionListIdContainer',
                tools: '',
                pageSize: 10,
                pageSizeLimit: [10, 15, 20]
            };
            var pltlogGrid1 = $.fn.dlshouwen.grid.init(pltlogOption1);
            pltlogGrid1.load();

        }
    })

    //初始化 文件 类别 类目
    var fileInstantiation = {
        classCateCode: 'WBLY',
        fileClassCode: 'KHHTHXY'
    }
    var fileInstantiationDisable = {
        classCateCodeDis: true, //文件类目不可修改
        fileClassCodeDis: true //文件类型不可修改
    }
    //调用初始化方法
    initFileUpload($$scope, fileInstantiation, fileInstantiationDisable, $CertusFormAjax);

    //比价单 单号输入后 的change函数
    $$scope.pcdCodeChange = function () {
        $CertusFormAjax.ajax("purchase/relatedSupplierName", {pcsCode: $$scope.agreement.pcsCode}, function (res) {
            if (res.data && res.data.length && res.data.length > 0) {
                $$scope.agreement.suppliernameSelect = res.data;
            } else {
                layer.msg("该比价单无法使用");
            }
        })
    }

    $$scope.affiliationChange = function(){

        for(var i in $$scope.select.affiliationSelect){
            if ($$scope.select.affiliationSelect[i].code == $$scope.agreement.affiliateCode) {
                $$scope.agreement.affiliateName = $$scope.select.affiliationSelect[i].name

                //取回合同 编号
                $CertusFormAjax.ajax("purchase/availableContractNum",{affiliation:$$scope.agreement.affiliateCode},function(res){
                    $$scope.agreement.contractCode = res.data;
                })
            }
        }
    }

    $$scope.select = {};
    $$scope.select.affiliationSelect = [{
        name: "南京",
        code: "STSNJ"
    },
        {
            name: "北京",
            code: "STSBJ"
        },
        {
            name: "广州",
            code: "STSGD"
        },
        {
            name: "上海",
            code: "STSSH"
        }
    ]

    //报价协议提交函数
    $$scope.agreementSubmit = function () {
        //判断 是否 有文件需要提交
        if (!$(".shuplod").is(":hidden")) {
            layer.msg("文件上传还没有操作完成。")
        }
        if (!$$scope.agreement.filePathMap) {
            layer.msg("请上传文件");
            return;
        }
        if (certusValidateNotnull()) {
            $CertusJsonAjax.ajax("purchase/agreementSubmit", $$scope.agreement, function (res) {
                if(res.data.indexOf("success")>-1){
                    layer.msg("合同编号已修订为:"+res.data.replace("success=",""));
                    findBacklogPersonHead($CertusFormAjax, "CGXYLC");
                    history.back();
                    return;
                }

            })
        }
    }

    //协议文档提交
    $$scope.agreementFileSubmit = function () {
        fileUpload($$scope,
            function (res) {

                var filePath = {
                    "fileId": res.data,
                    "fileName": $$scope.file.fileName,
                    "createTime": new Date()
                }
                $$scope.agreement.filePathMap = filePath;
                $$scope.agreement.filePath = JSON.stringify(filePath);
                shuplod();
                //alert(eval("("+$$scope.agreement.filePath+")"))
            },
            null)
    }
}

function agreementExamine($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $cookies, $routeParams, $q, fileListService) {
    $$scope = $scope;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;
    $fileListService = fileListService;

    $$scope.downloadAgreement = function(){
        $CertusFormAjax.download("downloadFile", "fileId=" + eval("("+$$scope.agreement.filePath+")").fileId);
    }

    $$scope.getsssbbb = function(bb){
        if(bb == "starting"){
            return "正在进行中";
        }else if(bb == "denied"){
            return "否决";
        }else if(bb == "sentback"){
            return "被退";
        }else if(bb == "others"){
            return "关闭";
        }else if(bb == "completed"){
            return "完成";    }else if(bb == "terminated"){        return "终结";    }    else if(bb == "cancelled"){        return "通过";}
        return "待办";
    }

    $CertusFormAjax.ajax("purchase/agreementInfoByCode",{agreementCode:$routeParams.agreementCode},function(res){
        var pltlogOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '请购清单',
            datas: res.data.buyingItem,
            columns: pltlogColumns,
            gridContainer: 'purchaselistContainer',
            toolbarContainer: 'purchaselistToolBarContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        var pltlogGrid = $.fn.dlshouwen.grid.init(pltlogOption);
        pltlogGrid.load();

        $$scope.buyingItems = res.data.buyingItem;

        $$scope.agreement = res.data.agreement;

        $$scope.processs = res.data.processs;

    })
}